package strings;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.*;

/**
 * @author moso
 * @Date 2025/6/7 18:43
 */

public class String2025 {
    /**
     * Sort a list of words based on number of characters
     * and ignore the asterisk separator between them
     * Example Input:
     * “This*is*aHelloWorldexampleto*demo*your****coding*ability“
     * Example Output:
     * “a is to This demo your Hello World coding ability example”
     * '''
     */

    public String getSplitString(String string){
        String[] strs = string.split("\\*");
        List<String> list = new ArrayList<>();
        for (String str : strs){
            if (!str.isEmpty()){
                list.add(str);
            }
        }
        Collections.sort(list, (a,b) -> Integer.compare(a.length(),b.length()));
        return String.join(" ", list);
    }

    @Test
    void testCompare(){
        String res = getSplitString("This*is*aHelloWorldexampleto*demo*your****coding*ability");
        System.out.println(res);
    }

    /**
     * Question:
     * Generate combined work report for team members from two different sources
     *
     * Example Input:
     * Map1/Dict1:
     * "Sunny" => "jira-6”, "jira-3", "jira-5", "jira-1"
     * "Bob" => "jira-10", "jira-1"
     *
     * Map2/Dict2:
     * "Bob" => "jira-1", "jira-2"
     * "Alice" => "jira-3", "jira-10", "jira-4"
     *
     *
     * Expected Output:
     * A display of JIRA tickets and the related owners
     * a)Jira ticket list in JIRA number order
     * b)For each JIRA the owner is in alphabetic order
     *
     * Example Output:
     * JIRA-1 is taken care by owner [Bob, Sunny]
     * JIRA-2 is taken care by owner [Bob]
     * JIRA-3 is taken care by owner [Alice, Sunny]
     * JIRA-4 is taken care by owner [Alice]
     * JIRA-5 is taken care by owner [Sunny]
     * JIRA-6 is taken care by owner [Sunny]
     * JIRA-10 is taken care by owner [Alice, Bob]
     */


    @Test
    void testJIRA(){
        // 模拟输入数据 Map1
        Map<String, List<String>> map1 = new HashMap<>();
        map1.put("Sunny", Arrays.asList("jira-6", "jira-3", "jira-5", "jira-1"));
        map1.put("Bob", Arrays.asList("jira-10", "jira-1"));

        // 模拟输入数据 Map2
        Map<String, List<String>> map2 = new HashMap<>();
        map2.put("Bob", Arrays.asList("jira-1", "jira-2"));
        map2.put("Alice", Arrays.asList("jira-3", "jira-10", "jira-4"));

        Map<String, Set<String>> map3 = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : map1.entrySet()){
            for (String jira : entry.getValue()){
                if (!map3.containsKey(jira)){
                    HashSet<String> nameSet = new HashSet<>();
                    nameSet.add(entry.getKey());
                    map3.put(jira,nameSet);
                }else {
                    map3.get(jira).add(entry.getKey());
                }
            }
        }
        for (Map.Entry<String, List<String>> entry : map2.entrySet()){
            for (String jira : entry.getValue()){
                if (!map3.containsKey(jira)){
                    HashSet<String> nameSet = new HashSet<>();
                    nameSet.add(entry.getKey());
                    map3.put(jira,nameSet);
                }else {
                    map3.get(jira).add(entry.getKey());
                }
            }
        }
        for (Map.Entry<String, Set<String>> entry : map3.entrySet()){
            System.out.println(entry.getKey().toString() + " is taken care by owner " + entry.getValue());
        }
        System.out.println();
    }

    /**
     * 判断是否是有效括号
     * ()[]{}  有效
     * ([{}])  有效
     */

    @Test
    void testKuohao(){
        String string = "()[]{}(";
        boolean res = false;
        Stack<Character> stack = new Stack<>();
        for (int i=0; i<string.length(); i++){
            char ch = string.charAt(i);
            if (ch == '('){
                stack.add(')');
            } else if (ch == '[') {
                stack.add('}');
            } else if (ch == '{') {
                stack.add('}');
            }else {
                if (stack.isEmpty() || ch != stack.pop()){
                    res = false;
                }
            }
        }
        if (stack.isEmpty()){
            res = true;
        }
        System.out.println(res);
    }
}
